import Cookies from 'js-cookie'

const TokenKey = 'Admin-Token'

import  { constantRoutes } from '@/router'

export function getToken() {
  return localStorage.getItem(TokenKey)
  // return Cookies.get(TokenKey)
}

export function setToken(token) {
  return localStorage.setItem(TokenKey,token)
}

export function removeToken() {
  return localStorage.removeItem(TokenKey)
}


// userInfo
const UserInfoKey = 'Admin-UserInfo'
export function getUserInfo() {
  return localStorage.getItem(UserInfoKey)
}

//新增的用户信息
export function getUserId() {
  let s =  localStorage.getItem(UserInfoKey)
  if(s) {
    let json = JSON.parse(s)
    return json['principal']['employee']['id']
  }
  return ''
}
export function getLoginName() {
  let s =  localStorage.getItem(UserInfoKey)
  if(s) {
    let json = JSON.parse(s)
    return json['principal']['employee']['loginName']
  }
  return ''
}
export function getName() {
  let s =  localStorage.getItem(UserInfoKey)
  if(s) {
    let json = JSON.parse(s)
    return json['principal']['employee']['name']
  }
  return ''
}

export function setUserInfo(token) {
  return localStorage.setItem(UserInfoKey,token)
}

export function removeUserInfo() {
  return localStorage.removeItem(UserInfoKey)
}

// 跳转到登录页
export function jumpLogin() {
  // window.location.href = "http://localhost:8080/sso/oauth/authorize?client_id=ActivityManagement&response_type=code&redirect_url=http://localhost/index"
  // window.location.href = "http://172.18.91.12:8888/sso/oauth/authorize?client_id=ActivityManagement&response_type=code&redirect_url=http://172.18.91.50:9008/index"
  // window.location.href='https://sso.worldskillsmuseum.cn/sso/logout'
  window.location.href = "http://172.18.91.12:8888/sso/logout";
}


export function setRouterList() {
  let a = localStorage.getItem('menu02')
  if(a) {
    let authorities = JSON.parse(a)
    constantRoutes.map(item => {
      if(item.children && item.children[0].path) {
        let url = item.path + '/' + item.children[0].path
        if(url == '/index') {
          return item
        }

        if(authorities && authorities.length > 0) {
          let a = false

          let curMenu = null
          authorities.map(it => {
            // console.log('b>' + it.url + '>' + url)
            if(it.url == url + ";35") {
              a = true
              curMenu = it
            }
            return it
          })
          console.log('a>' + a + '>' + url)

          if(a) {
            item.hidden = false
            item.children[0].meta.title = curMenu.name
          }
        }else {
          item.hidden = true
        }
      }
      //本地调试
      //item.hidden=false;
      return item
    });
  }

}
